package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.pojo.entity.SystemRoleMenu;
import com.lwc.cfdns.service.SystemRoleMenuService;
import com.lwc.cfdns.mapper.SystemRoleMenuMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spring
 * @description 针对表【cfdns_system_role_menu(角色和菜单关联表)】的数据库操作Service实现
 * @createDate 2023-05-24 09:51:48
 */
@Service
public class SystemRoleMenuServiceImpl extends ServiceImpl<SystemRoleMenuMapper, SystemRoleMenu>
        implements SystemRoleMenuService {

    /**
     * 权限处理
     *
     * @param role_id
     * @param menuList
     * @return boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 10:18:54
     */
    @Override
    public boolean rules(Long role_id, List<Long> menuList) {
        // 删除原来的权限
        this.baseMapper.delete(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getRoleId, role_id));
        if (menuList.size() > 0) {
            ArrayList<SystemRoleMenu> listRoles = new ArrayList<>();
            for (Long menu_id : menuList) {
                SystemRoleMenu systemRoleMenu = new SystemRoleMenu();
                systemRoleMenu.setMenuId(menu_id);
                systemRoleMenu.setRoleId(role_id);
                listRoles.add(systemRoleMenu);
            }
            // 批理插入
            this.saveBatch(listRoles);
        }
        return true;
    }

    /**
     * 获取角色的已选中菜单
     *
     * @param id
     * @return List<Long>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:32
     */
    @Override
    public List<Long> roleMenuTreeselect(Long id) {
        List<Long> list = new ArrayList<>();
        List<SystemRoleMenu> systemRoleMenus = this.baseMapper.selectList(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getRoleId, id));
        for (SystemRoleMenu systemRoleMenu : systemRoleMenus) {
            list.add(systemRoleMenu.getMenuId());
        }
        return list;
    }

    /**
     * 删除角色时删除对应菜单
     *
     * @param role_id
     * @return boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:47
     */
    @Override
    public boolean deletes(Long role_id) {
        // 删除原来的权限
        int res = this.baseMapper.delete(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getRoleId, role_id));
        return res != 0;
    }
}




